/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 62536
 * Date: 2024-06-21
 * Time: 22:05
 */
public class Solution {
    public static void main(String[] args) {
        System.out.println(longestPalindrome("aacabdkacaa"));
    }
    public static String longestPalindrome(String s) {
        int n = s.length();
        boolean[][] dp = new boolean[n][n];
        int begin = 0, len = 1;

        for (int i = n-1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                StringBuffer s2 = new StringBuffer();
                if (s.charAt(i) == s.charAt(j)) {
                    dp[i][j] = i + 1 >= j || dp[i + 1][j - 1];
                }

                if(dp[i][j] && j - i + 1 > len){
                    len = j-i+1;
                    begin = i;
                }

            }
        }

        return s.substring(begin, begin + len);

    }
}

